﻿package {
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	[SWF(width="800", height="600")]
	public class FileUploaderTest extends Sprite {
		public function FileUploaderTest() {
			init();
		}
		private var req:URLRequest;
		private var res:URLLoader;
		private function init():void {
			stage.addEventListener(MouseEvent.CLICK, onClickStage);
		}
		private function onClickStage(evt:MouseEvent):void {
			var url:String = stage.loaderInfo.parameters.apiUpload;
			if(url) {
				req = new URLRequest(url);
				res = new URLLoader();
				res.addEventListener(Event.COMPLETE, onUploadComplete);
				res.addEventListener(IOErrorEvent.IO_ERROR, onUploadError);
				res.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadError);
				var jpg:ByteArray = new ByteArray();
				var now:Date = new Date();
				jpg.writeUnsignedInt(now.getTime());
				FileUploader.uploadFiles(res, req, [jpg]);
			}
		}
		private function onUploadComplete(evt:Event):void {
			trace(evt.target.data);
		}
		private function onUploadError(evt:ErrorEvent):void {
			trace(evt.text);
		}
	}
}
